<?php
require 'include.php';
$search = (!empty($_REQUEST['search'])) ? $_REQUEST['search'] : "";
if($search){
	if(strlen($search) > 2){
		$found = 0;
		$q = "select membres.id as id, prenom,membres.nom as nom,  group_concat(equipes.nom separator ', ') as equipes from membres left join membre_equipes on membres.id = membre_id left join equipes on equipes.id = equipe_id where (membres.nom like '$search%' or prenom like '$search%') and membres.valide = 1 group by membres.id ";
		$r = ExecRequete($q);
		$i = 0;
		$lines = array();
		while($s = LigneSuivante($r)){
			$found = 1;
			$lines[] = "<div style = 'width: 240px; text-align: left; position: relative;' onclick = 'membre_detail($s->id);'>$s->prenom $s->nom ($s->equipes) <span class = 'float_right'>&gt;</span></div>\n";
			
		}
		if(count($lines)){
			echo "<p class = 'bold' style = 'margin-left: 10px'>Membres :</p>\n";
			echo "<div class = 'box'>\n";
			echo implode("<br class = 'clear' /><hr style = 'margin-left: -10px; margin-right: -10px; '/>\n",$lines);
			echo "<br class = 'clear' />\n";
			echo "</div>\n";
		}
		$q = "select id,no,nom,localite,adresse from salles where (nom like '%$search%' or localite like '%$search%' or no = '$search') and no != 0";
		$r = ExecRequete($q);
		$i = 0;
		$lines = array();
		while($s = LigneSuivante($r)){
			$found = 1;
			$lines[] = "<div style = 'width: 240px; text-align: left; position: relative;' onclick = 'self.location.href = \"http://maps.google.com/maps?q=".(str_replace(" ","+",$s->adresse))."+".(str_replace(" ","+",$s->localite))."+switzerland\"'><span class = 'bold'>".$s->no." : ".$s->nom."</span><br /> ".$s->adresse.", ".$s->localite."</div>";
		}
		if(count($lines)){
			echo "<p class = 'bold' style = 'margin-left: 10px'>Salles :</p>\n";
			echo "<div class = 'box'>\n";
			echo implode("<br class = 'clear' /><hr style = 'margin-left: -10px; margin-right: -10px; '/>\n",$lines);
			echo "<br class = 'clear' />\n";
			echo "</div>\n";
		}
		else echo "";
		if(!$found) echo "<p class = 'center fade'>Le r&eacute;sultat de la recherche est vide</p>\n";

	}
}
else{
	echo "<h2>Recherche:</h2>\n";
	echo "<form action  = 'recherche.php' method = 'post' id = 'search_form' name = 'search_form'>\n";
	echo  "<p class = 'center'><input type = 'search' name = 'search' value = 'membre ou salle' class = 'fade' onclick = 'if(this.value.indexOf(\"membre ou salle\") > -1){this.value = \"\"; this.className = \"\";}' onkeyup = 'if(this.value.length > 2){recherche(this.value);}' autocorrect='off' autocapitalize='off'/></p>\n";
	echo "</form>\n";
	echo "<div id = 'search_result'></div>\n";
}
?>